﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CQIE.OVS.BLL;
using CQIE.OVS.Model;
using CQIE.OVS.Web.Models;

namespace CQIE.OVS.Web.Controllers
{
    public class CamController : Controller
    {
        // GET: Cam
        public string DeleteCam(int Id)
        {
            CampaignerBLL bll =new CampaignerBLL();
            var model = bll.Get(Id);
            model.IsAlive = 0;
            bll.Update(model);
            return "success";
        }

        private Campaigner GetModel(CamModel Camdata)
        {
            Campaigner model = new Campaigner()
            {
                Activity = new ActivityBLL().Get(Camdata.ActiveId),
                Info = Camdata.Info,
                Sex = Camdata.Sex == "男" ? 1 : 0,
                Name = Camdata.Name,
                IsAlive = 1,
                ID = Camdata.Id
            };
            model.Birthday = model.GetBirthday(Camdata.Age);
            return model;
        }

        public ActionResult DetailCam(int Id)
        {
            Campaigner model = new CampaignerBLL().Get(Id);
            model.Age = model.GetAge(model.Birthday);
            return View(model);
        }

        public ActionResult Edit(int Id)
        {
            CampaignerBLL bll = new CampaignerBLL();
            Campaigner cam = bll.Get(Id);
            CamModel model = new CamModel()
            {
                Age = cam.GetAge(cam.Birthday),
                Id = cam.ID,
                Info = cam.Info,
                Name = cam.Name,
                Sex = cam.Sex==1?"男":"女",
                ActiveId = cam.Activity.ID
            };
            return View(model);
        }

        public void CreateCam(CamModel Camdata)
        {
            CampaignerBLL bll =new CampaignerBLL();
            bll.Create(GetModel(Camdata));
        }

        public void EditCam(CamModel Camdata)
        {
            CampaignerBLL bll = new CampaignerBLL();
            var model = GetModel(Camdata);
            bll.Update(model);
        }
    }
}